package com.exercises;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 给定一个包含 n 个整数的数组 nums，判断 nums 中是否存在三个元素 a，b，c ，使得 a + b + c = 0 ？找出所有满足条件且不重复的三元组。
 */
public class Exercise15 {

    public static void main(String[] args) {
        System.out.println(new Exercise15().letterCombinations("23"));
    }

    public List<String> letterCombinations(String digits) {
        List<String> result = new ArrayList<>();
        if (digits == null || digits.trim().length() == 0) {
            return new ArrayList<>();
        } else if (digits.length() == 1) {
            switch (Integer.parseInt(digits)) {
                case 2:
                    result = Arrays.asList("a", "b", "c");
                    break;
                case 3:
                    result = Arrays.asList("d", "e", "f");
                    break;
                case 4:
                    result = Arrays.asList("g", "h", "i");
                    break;
                case 5:
                    result = Arrays.asList("j", "k", "l");
                    break;
                case 6:
                    result = Arrays.asList("m", "n", "o");
                    break;
                case 7:
                    result = Arrays.asList("p", "q", "r", "s");
                    break;
                case 8:
                    result = Arrays.asList("t", "u", "v");
                    break;
                case 9:
                    result = Arrays.asList("w", "x", "y", "z");
                    break;
                default:
                    break;
            }

        } else if (digits.length() > 1) {
            List<String> list = letterCombinations(digits.substring(0, digits.length() - 1));
            String key = digits.substring(digits.length() - 1, digits.length());
            if (list.size() == 0) return letterCombinations(key);
            for (String l : list) {
                for (String k : letterCombinations(key)) {
                    result.add(l + k);
                }
            }
        }
        return result;
    }
}
